// pages/collection/collection.js
const app = getApp();
var service = require('../../util/service');
var util = require('../../util/util')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    head: {
      currentCity: "",
      placestr: '查找机构'
    },
    tabbar: {},
    selOrg:true,
    orgList: [],
    headline:[],
    informationList:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //调用app中的函数
    app.changeTabBar(); 
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    app.getpol(
        function (currentCity) {
          that.setData({
            'head.currentCity': currentCity
          })
        }
    )
    var that = this;

    var user = app.userInfo;
    var buyc = user.buyCollection;
    var proids = "";
    var topicids = "";
    if (buyc && buyc != "" && buyc != "null") {
      var buyobj = JSON.parse(buyc);
      if (buyobj) {
        proids = buyobj.provider||"";
        topicids = buyobj.topic||"";
      }
    }
    if (proids.indexOf(",") == 0) proids = proids.substr(1, proids.length);
    if (topicids.indexOf(",") == 0) topicids = topicids.substr(1, topicids.length);
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        that.setData({
          lat: res.latitude,
          lon: res.longitude,
        });
    //获取机构收藏
    if (proids != "")
    service.AJAX("smc/base/SystemProviderUser/listSystemProviderUser.json?$prvId-in=" + proids, {}, res => {
      var orgList = res;
      for (var i = 0; i < orgList.length; i++) {
        var price = orgList[i].minPrice + "";
        var activitys = orgList[i].prodLabel;
        if (activitys && activitys != "") {
          orgList[i].activity = activitys.split(/[；;]/);
        }
        var prvDesc = orgList[i].prvDesc;
        if (prvDesc && prvDesc.length > 30) {
          orgList[i].prvDesc = prvDesc.substr(0, 30) + "……";
        }
        orgList[i].attribute1 = service.imgurl + orgList[i].attribute1;
        orgList[i].minPrice = price.indexOf(".") > 0 ? price.split(".") : (price + ".00").split(".");
        orgList[i].distance = util.getDistance(that.data.lat, that.data.lon, orgList[i].posX, orgList[i].posY);
      }
      that.setData({
        orgList: orgList,
      })
    }, err => { }, 'GET')
      }
    });
    if (topicids!="")
    service.AJAX("smc/topic/NewTopics/listNewTopics.json?$topicId-in=" + topicids, {}, res => {
      var informationList = res;
      for (var i = 0; i < informationList.length; i++) {
        informationList[i].imgs = informationList[i].pic && informationList[i].pic.split(",");
        for (var j = 0; j < informationList[i].imgs.length; j++) {
          informationList[i].imgs[j] = service.imgurl + informationList[i].imgs[j];
        }
        informationList[i].createdAt = util.getTime(informationList[i].createdAt);
      }
      that.setData({
        informationList: informationList,
      })
    }, err => { }, 'POST')
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  selindex:function(){
    this.setData({
      'selOrg': true
    })
  },
  selindex2:function(){
    this.setData({
      'selOrg': false
    })
  }
})